package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;
import java.util.List;
import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.personInformationSetting.PersonInformationSettingVO;
import com.jwsoft.manager.common.vo.personInformationSetting.PersonInformationSettingKeyVO;
import com.jwsoft.manager.common.vo.personInformationSetting.PersonInformationSettingQueryVO;
import com.jwsoft.manager.core.integration.PersonInformationSettingIntegration;
import com.jwsoft.manager.core.dao.service.PersonInformationSettingService;
import com.jwsoft.manager.core.dao.model.PersonInformationSetting;
/**
 * 个人信息授权设置业务实现类
 *
 * @author chenzh
 * @since 2024-03-16
 */
@ApiService(funcCode = "personInformationSetting", title = "个人信息授权设置")
public class PersonInformationSettingIntegrationImpl implements PersonInformationSettingIntegration {

    @Autowired
    public PersonInformationSettingService personInformationSettingService;

    @Override
    @OpApi(funcCode = "personInformationSetting0001", title = "个人信息授权设置分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<PersonInformationSettingVO> getList(PersonInformationSettingQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(20);
        }
        LambdaQueryWrapper<PersonInformationSetting> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(vo.getUseType()),PersonInformationSetting::getUseType,vo.getUseType())
                .eq(StringUtils.isNotBlank(vo.getPersonInformationType()),PersonInformationSetting::getPersonInformationType,vo.getPersonInformationType())
                .like(StringUtils.isNotBlank(vo.getPersonInformationName()),PersonInformationSetting::getPersonInformationName,vo.getPersonInformationName());

        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<PersonInformationSetting> list = personInformationSettingService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<PersonInformationSetting> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<PersonInformationSettingVO> resultList = Convert.toList(PersonInformationSettingVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "personInformationSetting0002", title = "个人信息授权设置根据主键查询详情", funcType = FuncTypeEnum.query)
    public PersonInformationSettingVO getById(PersonInformationSettingKeyVO vo) {
        PersonInformationSetting entity = personInformationSettingService.getById(vo.getPersonInformationSettingId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        PersonInformationSettingVO result = Convert.convert(PersonInformationSettingVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "personInformationSetting0003", title = "个人信息授权设置保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(PersonInformationSettingVO vo) {
        if(StringUtils.isBlank(vo.getUseType())){
            throw new AppException("使用类型不能为空");
        }
        if(StringUtils.isBlank(vo.getPersonInformationName())){
            throw new AppException("授权类型名称不能为空");
        }
        if(StringUtils.isBlank(vo.getPersonInformationType())){
            throw new AppException("授权类型编码不能为空");
        }
        //主键为空为新增校验
        QueryWrapper<PersonInformationSetting> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(PersonInformationSetting::getUseType,vo.getUseType())
                .ne(vo.getPersonInformationSettingId()!=null,PersonInformationSetting::getPersonInformationSettingId,vo.getPersonInformationSettingId())
                .eq(PersonInformationSetting::getPersonInformationType,vo.getPersonInformationType());
        if (personInformationSettingService.count(queryWrapper)>0) {
            throw new AppException("授权类型编号不能重复");
        }
        QueryWrapper<PersonInformationSetting> queryWrapperName=new QueryWrapper<>();
        queryWrapperName.lambda().eq(PersonInformationSetting::getUseType,vo.getUseType())
                .ne(vo.getPersonInformationSettingId()!=null,PersonInformationSetting::getPersonInformationSettingId,vo.getPersonInformationSettingId())
                .eq(PersonInformationSetting::getPersonInformationName,vo.getPersonInformationName());
        if (personInformationSettingService.count(queryWrapperName)>0) {
            throw new AppException("授权类型名称不能重复");
        }
        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "personInformationSetting0004", title = "个人信息授权设置新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "personInformationSetting0004",
            operationName = "个人信息授权设置新增",
            dataType = "personInformationSettingId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public PersonInformationSettingVO add(PersonInformationSettingVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        PersonInformationSetting entity = Convert.convert(PersonInformationSetting.class, vo);
        personInformationSettingService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setPersonInformationSettingId(entity.getPersonInformationSettingId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "personInformationSetting0005", title = "个人信息授权设置修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "personInformationSetting0005",
            operationName = "个人信息授权设置修改",
            dataType = "personInformationSettingId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public PersonInformationSettingVO edit(PersonInformationSettingVO vo) {
        if (ObjectUtils.isEmpty(vo.getPersonInformationSettingId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        PersonInformationSetting oldEntity = personInformationSettingService.getById(vo.getPersonInformationSettingId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        PersonInformationSetting entity = Convert.convert(PersonInformationSetting.class, vo);
        personInformationSettingService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "personInformationSetting0006", title = "个人信息授权设置根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "personInformationSetting0006",
            operationName = "个人信息授权设置删除",
            dataType = "personInformationSettingId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(PersonInformationSettingKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getPersonInformationSettingId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        PersonInformationSetting entity = personInformationSettingService.getById(vo.getPersonInformationSettingId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        personInformationSettingService.removeById(vo.getPersonInformationSettingId());
    }
}
